<template>
	<div class="add-help-doc">
		<div>
			<label>{{$t('route.base')}}</label>
			<div></div>
			<button><img /><label>{{$t('route.Add_document')}}</label></button>
		</div>
		<div>
			<el-form label-width="100px">
				<el-form-item :label="$t('route.File_Type')">
					<el-radio-group v-model="fileType" @change='suffixChange'>
						<el-radio-button :label="$t('route.Picture')" />
						<el-radio-button :label="$t('route.video')" />
						<el-radio-button :label="$t('route.PDF')" />
					</el-radio-group>
				</el-form-item>
				<el-form-item :label="$t('route.document')" >
					<el-upload ref="uploadFile" :accept="accept" :limit="limit" drag :on-remove="onRemove"
						action="http://file.chinahikari.com:8686/hikari-cloud/file-mgr/base/uploadFile?type=3" multiple
						:on-success="uploadSuccess">
						<i class="el-icon-upload"></i>
						<div class="el-upload__text">{{$t('route.Drop_files_here')}}，{{$t('route.or')}}<em>{{$t('route.Click_Upload')}}</em></div>
						<div class="el-upload__tip" style="color: #FF0000;" slot="tip">{{$t('route.video_and_PDF_limit_pictures_limit')}}</div>
					</el-upload>
				</el-form-item>
				<el-form-item :label="$t('route.classification_of_equipment')">
					<el-select v-model="form.deviceCategory" style="width:300px" @change="deviceCategoryChange">
						<el-option v-for="item in deviceCategoryList" :label="item" :value="item" />
					</el-select>
					<el-select v-model="form.typeNum" style="width:300px;margin-left: 20px;">
						<el-option v-for="item in typeNumList" :label="item" :value="item" />
					</el-select>
				</el-form-item>
				<el-form-item :label="$t('route.title')">
					<el-input v-model="form.title" style="width:400px" />
				</el-form-item>
				<el-form-item :label="$t('route.Details')">
					<el-input type="textarea" v-model="form.detail" />
				</el-form-item>
				<el-form-item :label="$t('route.Type_of_document')">
					<el-radio-group v-model="form.docGroup">
						<el-radio-button v-for="item in docGroupList" :label="item" />
					</el-radio-group>
				</el-form-item>
				<el-form-item>
					<el-button @click='addClick'>{{$t('route.Add')}}</el-button>
				</el-form-item>
			</el-form>
		</div>


	</div>
</template>

<script>
	import http from "../../js/http/http.js";
	import api from "../../js/http/api.js";
	import CommonUtils from "../../js/common/common-utils.js";
	import i18n from '../../i18n/index';
	export default {
		data() {
			return {
				dvTypeList: [],
				typeNumList: [],
				deviceCategoryList: [],
				form: {
					title: null,
					suffix: 0,
					deviceCategory: null,
					typeNum: null,
					detail: null,
					docGroup: i18n.global.t('route.D_After_sales_related'),
				},
				fileType: i18n.global.t('route.Picture'),
				fileList: [],
				limit: 9,
				accept: 'image/*',
				docGroupList: [i18n.global.t('route.B_Production_related'), i18n.global.t('route.C_Inspection_related'), i18n.global.t('route.D_After_sales_related'), i18n.global.t('route.E_Application_training'), i18n.global.t('route.K_Reference_customers'), i18n.global.t('route.M_product_purchase_point')]
			};
		},
		methods: {
			uploadSuccess(response, file, fileList) {
				console.log(response.data)
				this.fileList.push(response.data);
			},
			deviceCategoryChange(value) {
				this.form.deviceCategory = value;
				this.typeNumList = [];
				for (var item of this.dvTypeList) {
					if (value === item.category) {
						this.typeNumList.push(item.number);
					}
				}
				this.typeNumList = [...new Set(this.typeNumList)];
				this.form.typeNum = null;
			},
			formatSuffix(suffix) {
				if (suffix == i18n.global.t('route.Picture')) {
					return 0;
				} else if (suffix == i18n.global.t('route.video')) {
					return 1;
				} else if (suffix == i18n.global.t('route.PDF')) {
					return 2;
				}
				return 0;
			},
			suffixChange(value) {
				console.log(value);
				this.form.suffix = this.formatSuffix(value);
				this.$refs.uploadFile.clearFiles();
				this.fileList = [];
				if (this.form.suffix == 0) {
					this.limit = 9;
					this.accept = 'image/*';
				} else if (this.form.suffix == 1) {
					this.limit = 1;
					this.accept = 'video/webm,video/ogg,video/3gp,video/mp4';
				} else {
					this.limit = 1;
					this.accept = 'application/pdf';
				}
			},
			onRemove(file, fList) {
				console.log(JSON.stringify(file));
				console.log(file.response.data);
				this.fileList.remove(file.response.data);
			},
			addClick() {
				console.log(this.fileList.length);
				if (CommonUtils.isEmpty(this.form.title)) {
					this.$message.error(i18n.global.t('route.Title_cannot_be_empty'))
					return;
				}
				if (CommonUtils.isEmpty(this.form.detail)) {
					this.$message.error(i18n.global.t('route.Details_cannot_be_empty'))
					return;
				}
				http.post(api.ADD_INSTRUCTION, {
					userId: -1,
					title: this.form.title,
					detail: this.form.detail,
					suffix: this.formatSuffix(this.form.suffix),
					docGroup: this.form.docGroup,
					url: JSON.stringify(this.fileList),
					deviceCategory: this.form.deviceCategory,
					typeNum: this.form.typeNum
				}, data => {
					this.$message.success(i18n.global.t('route.Successfully_added'))
				})
			},
			getDvCtgType() {
				http.post(api.GET_DV_CTG_TYPE, null, data => {
					for (var item of data.data) {
						this.deviceCategoryList.push(item.category);
					}
					this.deviceCategoryList = [...new Set(this.deviceCategoryList)];
					this.dvTypeList = data.data;
				});
			},


		},
		created() {
			this.getDvCtgType();
		}
	};
</script>
<style lang="scss" scoped>
	.add-help-doc {
		color: #444444;
		font-size: 18px;
		padding-right: 20px;
		height: calc(100vh - 100px);
		// overflow: scroll;
		scrollbar-width: none;
		/* firefox */
		-ms-overflow-style: none;
		/* IE 10+ */
		overflow-x: hidden;
		overflow-y: auto;


		>div:nth-of-type(1) {
			display: flex;
			flex-direction: row;
			align-items: center;
			margin-top: 20px;
			background-color: #ffffff;
			padding: 20px;
			border-radius: 5px;

			>label:nth-of-type(1) {
				font-weight: 600;
			}

			>div:nth-of-type(1) {
				width: 1px;
				background-color: #cccccc;
				height: 15px;
				margin: 0px 20px;
			}

			>button:nth-of-type(1) {
				background-image: url(../../assets/img/customer-1.png);
				background-repeat: no-repeat;
				background-size: 100% 100%;
				padding: 5px 10px;
				border: 0px;
				display: flex;
				color: #ffffff;
				flex-direction: row;
				align-items: center;
				font-weight: 800;

				>img {
					margin-right: 7px;
					width: 15px;
					height: 15px;
					content: url(../../assets/img/customer-2.png);
				}

			}
		}

		>div:nth-of-type(2) {
			display: flex;
			flex-direction: row;
			align-items: center;
			margin-top: 20px;
			background-color: #ffffff;
			padding: 20px;
			border-radius: 5px;

			.input {}

			.div-1 {
				display: flex;
				flex-direction: row;

				>label {
					margin-left: 10px;
					width: 200px;
				}
			}


		}





	}
</style>
